<?php
	foreach($data  as $survey){
		//survey
		echo "INSERT INTO surveys(id, name) VALUES(";
		echo $survey['Survey']['id'] . ", ";
		echo $survey['Survey']['name'] . ");\n";
	
		//items
		foreach($survey['Item'] as $item){
			echo "INSERT INTO items(id, survey_id, prompt, returns, next) VALUES(";
			echo $item['id'] . ", ";
			echo $item['survey_id'] . ", ";
			echo $item['prompt'] . ", ";
			echo $item['returns'] . ", ";
			echo $item['next'] . ");\n";
		}
	
		//results
		echo "CREATE TABLE results" . $survey['Survey']['id'];
		echo "( id INTEGER PRIMARY KEY, ";
		$not_first = false;
		foreach($survey['Item'] as $item){
			$returns = explode(',', $item['returns']);
			foreach($returns as $index=>$return){
				if($not_first)
					echo ", ";
				else
					$not_first = true;
				
				echo "q" . $item['id'] . '_' . $index;
				switch((int)$return){
					case 0:
						echo " INTEGER";
						break;
					case 1:
						echo " TEXT";
				}
			}
		}
		echo ");\n";
	}
?>
<!--END-->
